﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using DAL;

namespace BlogWebSite.Controllers
{
    public class BlogController : Controller
    {

        public ActionResult Index(string id)
        {
            var blog = DB.GetBlog(id);

            return View(blog);
        }
        [HttpPost]
        public ActionResult AddBlogRoll(int id, BlogRoll blogRoll)
        {
            blogRoll.Deleted = false;
            DB.CreateBlogRoll(id, blogRoll);
            return RedirectToAction("Index", "Blog", new {id = "mvc_blog"});
        }

        public ActionResult CreatePost()
        {
            return View(new BlogPost());
        }

        [HttpPost]
        public ActionResult CreatePost(BlogPost post)
        {
            if (post == null)
                post = new BlogPost();
            post.Created = DateTime.Now;
            int saved = 0;
            saved = DB.CreateBlogPost(3, post); //post.BlogPostedIn.BlogId, suppose to get from sesssion the blogownsers id, but hardcoded atm
            if (saved == 0)
                return View(post);
            else
                return RedirectToAction("Index", "Blog", new { id = "mvc_blog" });
        }

        public ActionResult EditUser(int id)
        {
            var user = DB.GetUser(id);
            return View(user);
        }

        [HttpPost]
        public ActionResult EditUser(int id, User user)
        {
            var updatedUser = DB.GetUser(id);
            updatedUser.Name = user.Name;
            updatedUser.Country = user.Country;
            updatedUser.Password = user.Password ?? updatedUser.Password;
            updatedUser.Blog.Description = user.Blog.Description;
            DB.UpdateUser(updatedUser);
            return View(updatedUser);
        }

        public ActionResult SingleBlogPost(int id)
        {
            var post = DB.GetBlogPost(id);
            return View(post);
        }


        [HttpPost]
        public ActionResult SingleBlogPost(int id, BlogComment blogComment)
        {
            blogComment.Created = DateTime.Now;
            blogComment.Deleted = false;
            DB.CreateBlogComment(id, blogComment);

            return RedirectToAction("SingleBlogPost", "Blog", new { id });
        }

        [HttpGet]
        public ActionResult DeleteComment(int id)
        {
            DB.DeleteBlogComment(id);
            var blogComment = DB.GetBlogComment(id);
            return RedirectToAction("SingleBlogPost", "Blog", new { id = blogComment.BlogPostCommentedOn.BlogPostId });
        }

        [HttpGet]
        public ActionResult EditPost(int id)
        {
            var post = DB.GetBlogPost(id);
            return View(post);
        }

        [HttpPost]
        public ActionResult EditPost(int id, BlogPost blogPost)
        {
            var post = DB.GetBlogPost(id);
            post.Title = blogPost.Title;
            post.PostText = blogPost.PostText;
            DB.UpdateBlogPost(post);
            return RedirectToAction("Index", "Blog", new { id = "mvc_blog" });
        }

        public ActionResult DeletePost(BlogPost post)
        {
            DB.DeleteBlogPost(post.BlogPostId);
            return RedirectToAction("Index", "Blog", new { id = "mvc_blog" });
        }

        public ActionResult DeleteBlogRollLink(int id)
        {
            DB.DeleteBlogRoll(id);
            return RedirectToAction("Index", "Blog", new {id = "mvc_blog"});
        }
    }
}